#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <sys/stat.h>

#include "syscomm.h"
#include "generaldef.h"

namespace syscomm{
	char commbuf[COMMBUF_LEN];
	void clearbuf(){
		memset(commbuf,0,sizeof(commbuf));
	}
	void copyfile(const char* prefix1, const char* path1, const char* suffix1,
			const char* prefix2, const char* path2, const char* suffix2){
		clearbuf();
		sprintf(commbuf,"cp %s%s%s %s%s%s",prefix1,path1,suffix1,prefix2,path2,suffix2);
		system(commbuf);
	}
	void movefile(const char path1[PATH_LEN], const char path2[PATH_LEN]){
		clearbuf();
		sprintf(commbuf,"mv %s %s",path1,path2);
		system(commbuf);
	}
	void delfile(const char target[PATH_LEN]){
		clearbuf();
		sprintf(commbuf,"rm %s",target);
		system(commbuf);
	}
	void createdir(const char target[PATH_LEN]){
		mkdir(target,S_IREAD | S_IWRITE | S_IEXEC | S_IRWXO);
	}
	void removedir(const char target[PATH_LEN]){
		clearbuf();
		sprintf(commbuf,"rm -r %s",target);
		system(commbuf);
	}
	void showcenalog(){
		system("cat score.log");
		system("cat report.log");
	}
	void compile(const char compiler[NAME_LEN], const char srcdir[PATH_LEN], const char srcname[NAME_LEN],
			const char targetdir[PATH_LEN]=NULL, const char targetname[NAME_LEN]=NULL){
		clearbuf();
		if(targetdir==NULL)
			sprintf(commbuf,"%s %s%s",compiler,srcdir,srcname);
		else
			sprintf(commbuf,"%s %s%s -o %s%s",compiler,srcdir,srcname,targetdir,targetname);
		system(commbuf);
	}
}
